package com.finger.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.finger.eduservice.entity.EduComment;
import com.finger.eduservice.entity.EduCourse;
import com.finger.eduservice.entity.EduCourseDescription;
import com.finger.eduservice.entity.frontvo.CourseWebVo;
import com.finger.eduservice.entity.frontvo.QueryCourseFront;
import com.finger.eduservice.entity.vo.CourseInfoVO;
import com.finger.eduservice.entity.vo.CoursePublishVO;
import com.finger.eduservice.exception.GuliException;
import com.finger.eduservice.mapper.EduCourseMapper;
import com.finger.eduservice.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-01-06
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired
    private EduCourseDescriptionService eduCourseDescriptionService;

    @Autowired
    private EduChapterService eduChapterService;

    @Autowired
    private EduVideoService eduVideoService;

    @Autowired
    private EduCommentService eduCommentService;

    @Override
    public String addCourseAndDescription(CourseInfoVO courseInfoVO) {
        EduCourse eduCourse = new EduCourse();

        BeanUtils.copyProperties(courseInfoVO,eduCourse);

        int resultCount = baseMapper.insert(eduCourse);

        if(resultCount==0){
            throw new GuliException(20001,"添加失败");
        }

        String id = eduCourse.getId();

        EduCourseDescription eduCourseDescription = new EduCourseDescription();

        eduCourseDescription.setDescription(courseInfoVO.getDescription());

        eduCourseDescription.setId(id);

        eduCourseDescriptionService.save(eduCourseDescription);

        return id;
    }

    @Override
    public CourseInfoVO getCourseInfo(String courseId) {

        CourseInfoVO courseInfoVO = new CourseInfoVO();

        EduCourse eduCourse = baseMapper.selectById(courseId);

        BeanUtils.copyProperties(eduCourse,courseInfoVO);

        EduCourseDescription eduCourseDescription = eduCourseDescriptionService.getById(courseId);

        BeanUtils.copyProperties(eduCourseDescription,courseInfoVO);

        return courseInfoVO;
    }

    @Override
    public void updateCourseInfo(CourseInfoVO courseInfoVO) {
        EduCourse eduCourse = new EduCourse();

        BeanUtils.copyProperties(courseInfoVO,eduCourse);

        int resultCourseRows = baseMapper.updateById(eduCourse);

        if(resultCourseRows==0){
            throw new GuliException(20001,"保存课程信息失败");
        }

        EduCourseDescription eduCourseDescription = new EduCourseDescription();

        BeanUtils.copyProperties(courseInfoVO,eduCourseDescription);

        boolean resultDescRows = eduCourseDescriptionService.updateById(eduCourseDescription);

        if(!resultDescRows){
           throw new GuliException(20001,"保存详细信息失败");
        }

    }

    @Override
    public CoursePublishVO getCoursePublish(String courseId) {
        return baseMapper.getCoursePublish(courseId);

    }

    @Override
    public void publishCourse(String courseId) {
        EduCourse eduCourse = new EduCourse();

        eduCourse.setId(courseId);

        eduCourse.setStatus("Normal");

        baseMapper.updateById(eduCourse);
    }

    @Override
    public void removeCourseById(String courseId) {

        eduVideoService.removeVideoByCourseId(courseId);

        eduChapterService.removeChapterById(courseId);

        eduCourseDescriptionService.removeById(courseId);

        baseMapper.deleteById(courseId);

    }
    // key可选属性，可以使用 SpEL 标签自定义缓存的key
    // value 缓存名，必填，它指定了你的缓存存放在哪块命名空间
    @Cacheable(value = "course",key = "'indexCourseList'")
    @Override
    public List<EduCourse> selectIndexCourseList() {
        // 获取根据访问量获取前八个课程
        QueryWrapper<EduCourse> eduCourseQueryWrapper = new QueryWrapper<>();
        eduCourseQueryWrapper.orderByDesc("view_count");
        eduCourseQueryWrapper.last("limit 8");
        List<EduCourse> eduCourses = baseMapper.selectList(eduCourseQueryWrapper);
        return eduCourses;
    }

    @Override
    public List<EduCourse> getCourseInfoByTeacherId(String id) {

        QueryWrapper<EduCourse> eduCourseQueryWrapper = new QueryWrapper<>();

        eduCourseQueryWrapper.eq("teacher_id",id);

        List<EduCourse> courses = baseMapper.selectList(eduCourseQueryWrapper);

        return courses;
    }

    @Override
    public Map<String, Object> getCourseListCondition(Page<EduCourse> eduCoursePage, QueryCourseFront queryCourseFront) {

        QueryWrapper<EduCourse> eduCourseQueryWrapper = new QueryWrapper<>();

        String title = queryCourseFront.getTitle();
        String subjectParentId = queryCourseFront.getSubjectParentId();
        String subjectId = queryCourseFront.getSubjectId();
        String teacherId = queryCourseFront.getTeacherId();
        String gmtCreateSort = queryCourseFront.getGmtCreateSort();
        String priceSort = queryCourseFront.getPriceSort();
        String buyCountSort = queryCourseFront.getBuyCountSort();

        if(!StringUtils.isEmpty(title)){
            eduCourseQueryWrapper.like("title",title);
        }
        if(!StringUtils.isEmpty(subjectParentId)){
            eduCourseQueryWrapper.eq("subject_parent_id",subjectParentId);
        }
        if(!StringUtils.isEmpty(subjectId)){
            eduCourseQueryWrapper.eq("subject_id",subjectId);
        }
        if(!StringUtils.isEmpty(teacherId)){
            eduCourseQueryWrapper.eq("teacher_id",teacherId);
        }
        if(!StringUtils.isEmpty(gmtCreateSort)){
            eduCourseQueryWrapper.orderByDesc("gmt_create");
        }
        if(!StringUtils.isEmpty(priceSort)){
            eduCourseQueryWrapper.orderByDesc("price");
        }
        if(!StringUtils.isEmpty(buyCountSort)){
            eduCourseQueryWrapper.orderByDesc("buy_count");
        }

        IPage<EduCourse> eduCourseIPage = baseMapper.selectPage(eduCoursePage, eduCourseQueryWrapper);

        Map map = new HashMap<String,Object>();
        long pages = eduCourseIPage.getPages();
        long current = eduCourseIPage.getCurrent();
        List<EduCourse> records = eduCourseIPage.getRecords();

        for (EduCourse record : records) {
            String courseId = record.getId();

            QueryWrapper<EduComment> commentQueryWrapper = new QueryWrapper<>();

            commentQueryWrapper.eq("course_id",courseId);

            int count = eduCommentService.count(commentQueryWrapper);

            record.setCommentCount(count);
        }
        long size = eduCourseIPage.getSize();
        long total = eduCourseIPage.getTotal();

        boolean hasNext  = true;
        boolean hasPrevious  = true;

        if(current == pages){
            hasNext = false;
        }
        if(current == 1){
            hasPrevious = false;
        }

        map.put("pages",pages);
        map.put("current",current);
        map.put("records",records);
        map.put("size",size);
        map.put("total",total);
        map.put("hasNext",hasNext);
        map.put("hasPrevious",hasPrevious);

        return map;
    }

    @Override
    public CourseWebVo getCourseWebVoById(String courseId) {
        return baseMapper.getCourseWebVoById(courseId);
    }

    @Override
    public void updateCourseBuyCount(String courseId) {
        baseMapper.updateCourseBuyCount(courseId);
    }
}
